package extras.mak001;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class Notifications {
	private static String script = "";
	private static String user = "mak001";

	public static int hasNotification() {
		String s = getNotification(0);
		if (s.contains("0")) {
			return 0;
		} else if (s.contains("1")) {
			return 1;
		} else if (s.contains("2")) {
			return 2;
		}
		return 3;
	}

	private static String notif() {
		return getNotification(1);
	}

	private static String getNotification(int type) {
		String s = "6";
		try {
			URL url;
			URLConnection urlConn;
			if (type == 0) {
				url = new URL(
						"http://mak001.x10.mx/dynamicsig/getNotifications.php");
			} else {
				url = new URL(
						"http://mak001.x10.mx/dynamicsig/getNotifString.php");
			}

			urlConn = url.openConnection();
			urlConn.setRequestProperty("User-Agent", "| supersecret |");
			urlConn.setDoOutput(true);
			urlConn.setDoInput(true);
			urlConn.setUseCaches(false);
			urlConn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			String content = "script=" + script + "&uname=" + user;
			DataOutputStream wr = new DataOutputStream(urlConn
					.getOutputStream());

			wr.writeBytes(content);
			String line;
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
			while ((line = rd.readLine()) != null) {
				s = line;
			}
			wr.flush();
			wr.close();
			rd.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}

	public static void showNotifications() {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					JOptionPane.showMessageDialog(null, notif(),
							"Notification", JOptionPane.PLAIN_MESSAGE);
					resetNotif(user, 0);
				}
			});
		} catch (InterruptedException e) {
		} catch (InvocationTargetException e) {
		}
	}

	private static void resetNotif(String user, int i) {
		try {
			URL url;
			URLConnection urlConn;

			url = new URL(
					"http://mak001.x10.mx/dynamicsig/resetNotifications.php");

			urlConn = url.openConnection();
			urlConn.setRequestProperty("User-Agent", "| supersecret |");
			urlConn.setDoOutput(true);
			urlConn.setDoInput(true);
			urlConn.setUseCaches(false);
			urlConn.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			String content = "script=" + script + "&uname=" + user + "&num="
					+ i;
			DataOutputStream wr = new DataOutputStream(urlConn
					.getOutputStream());

			wr.writeBytes(content);
			String line;
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
			while ((line = rd.readLine()) != null) {
				Logger.getLogger("s").info(line);
			}
			wr.flush();
			wr.close();
			rd.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void showGlobalNotification(final String user) {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					JOptionPane.showMessageDialog(null, globalNotif(),
							"Global Notification", JOptionPane.PLAIN_MESSAGE);
					resetNotif(user, 1);
				}

			});
		} catch (InterruptedException e) {
		} catch (InvocationTargetException e) {
		}
	}

	private static String globalNotif() {
		String s = "";
		try {
			URL notif = new URL(
					"http://mak001.x10.mx/dynamicsig/globalNotifications.txt");
			URLConnection urlConn = notif.openConnection();
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
			String line;
			while ((line = rd.readLine()) != null) {
				String j = line.replace("eol", System
						.getProperty("line.separator"));
				if (j.contains("@" + script)) {
					s = j.replace("@"+script+" ", "");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}
	
	public static void setInfo(String sname, String uname){
		script = sname;
		user = uname;
	}
}
